/*t图片盒子*/
let img_box = $("#article-1-box");
/*本地存储所有篇章*/
let chater = null;
/*本地存储所有篇章 数据长度*/
let chater_length = 0;
/*储存漫画 篇章 图片*/
let name_box = {};
/*第几章*/
let page = 0;
/*图片 页数*/
const img_page = 5;
/*总长度*/
let img_le = null;
/*每次加载的长度*/
let img_lenght = 0;
/*每次加载的长度 起始长度*/
let i_img = -5;
/*漫画标识*/
let nameId = 0;
/*开关 防止aja请求两次*/
let switc = true;
/*滑动监听 存储数据*/
let scroll_numder = 0;
/*正序true 倒序false*/
let change_switch = true;
/*导航来篇章名称 存储*/
let chater_name_json = {};

/*滑动监听*/
$("#mescroll").scroll(function () {

    /*滚轮高度*/
    let scrolltop = $("#mescroll").scrollTop();

    if (scrolltop > scroll_numder) {
        /*关闭*/
        $("#bottom,#top").slideUp("slow");
        scroll_numder = scrolltop;
        charter_name()
    } else {
        /*展开*/
        $("#bottom,#top").slideDown("slow");
        scroll_numder = scrolltop;
        chater_name();
    }

})


/*上翻 动态改变 上导航 篇章名称*/
let chater_name = () => {
    let topcnter = $("#top-centre");
    for (let i in chater_name_json) {
        if (scroll_numder > chater_name_json[i].min && scroll_numder <= chater_name_json[i].max) {
            if (topcnter.text() != chater_name_json[i].name) {
                topcnter.text(chater_name_json[i].name);
            }
        }
    }
}

/*下翻 动态改变 上导航 篇章名称*/
let charter_name = () => {
    let top_cnter = $("#top-centre");
    for (let i in chater_name_json) {
        if (scroll_numder >= chater_name_json[i].min) {
            if (top_cnter.text() != chater_name_json[i].name) {
                top_cnter.text(chater_name_json[i].name);
            }
        }
    }
}

/* 监听页面点击时间*/
$("#mescroll").click(function () {
    $("#bottom,#top").slideToggle("slow");
})
/*返回上一个页面*/
let previous = () => {
    window.open('/manhua/look/over/' + nameId + '');
}

/*下*/
let Un = () => {
    if (page >= chater_length) {
        $.modal.msgWarning('已经是最后一张章了^_^...');
        return;
    }
    location.href = '/manhua/look/picture/' + nameId + '/' + (++page);
}
/*上*/
let on = () => {
    if (page <= 1) {
        $.modal.msgWarning('已经是第一张章了^_^...');
        return;
    }
    location.href = '/manhua/look/picture/' + nameId + '/' + (--page);
}




/*打开 选集目录*/
let catalogueClick = () =>{

    $('#catalogue').slideDown('slow');

    $(".changeClickAll").css("color","#333333");

    $(".nameOrder-"+page).css({"color":"#337ab7"});

    $('#catalogue-scroll').animate({scrollTop:($(".nameOrder-"+page).offset().top - 800)},10);


}
/*关闭 选集目录*/
let closeCatalogueClick = () =>{

    $('#catalogue').slideUp('slow');

    $('#catalogue-scroll').animate({scrollTop:($(".nameOrder-"+page).offset().top - 800)},10);

}

/*正序 倒序*/
let changeClick = () => {

    let html = '';

    if (chater != null) {
        if (change_switch === true) {
            for (let key in chater) {
                html += `<a href="/manhua/look/picture/${chater[key].nameId}/${chater[key].nameOrder}"  class="changeClickAll nameOrder-${chater[key].nameOrder}">${chater[key].name}</a>`;
            }
            change_switch = change_switch ? false : true;
            $("#catalogue-div-cn").html('正序<i class="fa fa-angle-down"></i>');
        } else {
            for (let i = chater_length; i >= 1; i--) {
                html += `<a href="/manhua/look/picture/${chater['' + i + ''].nameId}/${chater['' + i + ''].nameOrder}" class="changeClickAll nameOrder-${chater['' + i + ''].nameOrder}">${chater['' + i + ''].name}</a>`;
            }
            change_switch = change_switch ? false : true;
            $("#catalogue-div-cn").html('倒序<i class="fa fa-angle-up"></i>');
        }

    }
    $("#catalogue-scroll").html(html);
}

/*拼装数据*/
let assemble = () => {

    /*起始页数加5*/
    i_img = i_img + img_page;
    /*终止页数加5*/
    img_lenght = img_lenght + img_page;


    let html = '';
    for (let i = i_img, len = img_lenght; i < len; i++) {
        if(name_box[page][i] === null || name_box[page][i] === '' || name_box[page][i] === undefined){
            return html;
        }
        html += `<img src="${$.haHa.decode(name_box[page][i])}" onerror="$.common.errorimg(this)">`;
    }

    return html;
}
/*请求图片*/
let chater_data = (boot) => {

    if (chater_name_json[page] != undefined) {
        chater_name_json[page] = {
            min: chater_name_json[page].min,
            max: scroll_numder,
            name: chater_name_json[page].name
        };
    } else {
        chater_name_json[page] = {min: scroll_numder, name: chater['' + page + ''].name};
    }

    let data = {}
    /*漫画 标识*/
    data['nameId'] = nameId;
    /*查看是 下拉 还是 上拉*/
    page = boot ? page + 1 : page - 1;
    data['pageId'] = chater[page].id;


    if (chater_name_json[page] != undefined) {
        chater_name_json[page] = {
            min: chater_name_json[page].min,
            max: scroll_numder,
            name: chater_name_json[page].name
        };
    } else {
        chater_name_json[page] = {min: scroll_numder, name: chater['' + page + ''].name};
    }

    $.operate.request(picture_url.prefix + "/" + picture_url.data_url, data, function (row) {
        /*关闭遮罩层*/
        $.modal.closeLoading();
        if (row.code === web_status.SUCCESS) {
            name_box[page] = row.data;
            /*篇章总长度*/
            img_le = row.data.length;
            /*恢复 初始默认值*/
            i_img = -5;
            img_lenght = 0;
            /*开关 开启 可以请求*/
            switc = true;
            /*上导航 篇章名称添加*/
            $("#top-centre").text(chater['' + page + ''].name)
            //img_box.empty();
            img_box.append(assemble());
        } else {
            $.modal.alertError(row.msg);
        }
    });
}
